package com.me.forum.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.me.enums.RespType;
import com.me.forum.domain.Plate;
import com.me.forum.request.PlateRequest;
import com.me.forum.service.impl.PlateService;
import com.me.forum.utils.components.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/plate")
public class AdminPlateController {
    @Autowired
    private PlateService plateService;

    @GetMapping("/{id}")
    public IModel getById(@PathVariable String id, IModel model) {
        Plate plate = plateService.findById(id);
        model.success(plate!=null);
        model.add(RespType.Plate, plate);
        return model;
    }

    //条件获取 Get
    @GetMapping
    public IModel getAllPlate(PlateRequest rreq, IModel model, String page, String size) {
        IPage<Plate> plates = plateService.getAll(rreq,page,size);
        model.success(plates!= null && plates.getSize() != 0);
        model.add(RespType.Plates, plates);
        return model;
    }

    @PostMapping
    public IModel addPlate(@RequestBody PlateRequest rreq, IModel model) {
        model.success(plateService.addPlate(rreq));
        return model;
    }

    @PutMapping
    public IModel updatePlate(@RequestBody PlateRequest rreq, IModel model) {
        model.success(plateService.updateByPlate(rreq));
        return model;
    }
    @DeleteMapping
    public IModel deletePlate(String id,IModel model) {
        model.success(plateService.delete(id));
        return model;
    }

}
